/**
 * Created by meiguizhinian on 2018/7/17
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//单例/全静态/枚举
ivy.GameObjectManager = {
	getInstance: function () {
		return this;
	},

	//通过actorID来创建对象
	createGameObjectByActorID : function(actorID) {
		if (actorID >= 0) {
			let sdm = ivy.SceneDataManager.getInstance();
			let classID = sdm.getObjectBaseInfo(actorID, ivy.BaseObject.ObjectBaseInfo.eClassID);
			let aiType = sdm.getObjectClassAIID(classID);
			let obj= this.createGameObjectByAIID(aiType);
			if (obj) {
				obj.initWithActorID(actorID);
				obj.initAfterCreate();
				return obj;
			}
		}
		return null;
	},
	//通过AIType来创建对象
	createGameObjectByAIID : function(aiType) {
		return this.objCreateFuncMap_.get(aiType) || null;
	},
	//创建所有场景中对象(用于场景初始化时对象的初始化)
	createAndAddAllSceneGameObejctToSceneNode : function() {
		let sdm = ivy.SceneDataManager.getInstance();
		let objCount = sdm.getSceneObjectCount();
		for (let i = 0; i < objCount; ++i) {
			let obj = this.createGameObjectByActorID(i);
			this.addGameObjectToScene(obj);
		}
	},
	//移除对象(注意源对象不会释放，仅随场景释放时释放)
	removeGameObject : function(obj) {
		this.allGameObjectMap_.erase(obj);
		this.unRegisterActivedGameObject(obj);
		if (obj.getObjectActorID() >= 0) {

		} else {
			obj.setVisible(false);
			//针对克隆对象,xxxxx
			ivy.Tools.pushFunctionToCocosThread(function() {
				obj.removeFromParent();
			});
		}
	},
	//清除所有对象信息(注意对象无需手动释放,随场景一起释放)
	clear : function() {
		this.allGameObjectMap_.clear();
		this.allSrcGameObjectMap_ = {};
		this.allRegistedGameObjectMap_ = {};
		this.allRegistedCollidableGameObjectMap_ = {};
		this.sceneObjTopNode_ = null;
		this.sceneObjNode_ = null;
	},
	//将对象添加到场景
	addGameObjectToScene : function(obj) {
		if (obj && this.sceneObjNode_)
		{
			this.sceneObjNode_.addChild(obj);
			let actorID = obj.getObjectActorID();
			if (actorID >= 0 && obj.getObjectLinkActorID()<0) {
				this.allSrcGameObjectMap_[actorID] = obj;
			}
			this.allGameObjectMap_.set(obj, obj);
		}
	},

	init : function(sceneNode) {
		let gc = ivy.GlobleConfig.getInstance();
		this.sceneObjTopNode_ = new cc.Node();
		this.sceneObjTopNode_.setCascadeColorEnabled(true);
		this.sceneObjTopNode_.setCascadeOpacityEnabled(true);
		sceneNode.addChild(this.sceneObjTopNode_);
		let sceneObjectNode = new cc.Node();
		sceneObjectNode.setCascadeColorEnabled(true);
		sceneObjectNode.setCascadeOpacityEnabled(true);
		sceneObjectNode.setAnchorPoint(cc.p(0.5, 0.5));
		sceneObjectNode.setPosition(gc.getScreenHalfWidth(), gc.getScreenHalfHeight());
		sceneObjectNode.setContentSize(cc.size(gc.getScreenWidth(), gc.getScreenHeight()));
		this.sceneObjTopNode_.addChild(sceneObjectNode);
		this.setSceneObjectNode(sceneObjectNode);
	},
	//设置对象节点
	setSceneObjectNode : function(node) {
		this.sceneObjNode_ = node;
	},
	//获取对象节点
	getSceneObjectNode : function() {
		return this.sceneObjNode_;
	},

	getSceneObjectTopNode : function() {
		return this.sceneObjTopNode_;
	},
	//注册对象创建函数
	registerObjCreateFunc : function(AITypeV, func, args) {
		cc.log("registerObjCreateFunc:",AITypeV, func, args);
		this.objCreateFuncMap_.set(AITypeV, func);
		if (args) {
			this.registerObjCreateFunc.apply(this, Array.prototype.slice.call(arguments, 2),);
		}
	},
	//获取源对象通过ActorID
	getSrcObjectByActorID : function(actorID) {
		return this.allSrcGameObjectMap_[actorID];
	},
	//注册当前活动对象
	registerActivedGameObject : function(obj) {
		this.allRegistedGameObjectMap_[obj] = obj;
	},
	//注销当前活动对象
	unRegisterActivedGameObject : function(obj) {
		if (this.allRegistedGameObjectMap_[obj]) {
			this.allRegistedGameObjectMap_[obj] = null;
		}

		//克隆对象处理
		if (obj.getObjectActorID() > 0 && obj.getObjectLinkActorID() > 0) {
			if (this.allGameObjectMap_.erase(obj)) {
				//针对克隆对象
				obj.setVisible(false);
				obj.runAction(cc.sequence(cc.delayTime(1.0), cc.removeSelf()));
			}
		}
	},
	//注册当前可碰撞对象
	registerCollidableGameObject : function(obj) {
		this.allRegistedCollidableGameObjectMap_[obj] = obj;
	},
	//注销当前可碰撞对象
	unRegisterCollidableGameObject : function(obj) {
		this.allRegistedCollidableGameObjectMap_[obj] = null;
	},

	getAllSrcGameObjectMap : function() {
		return this.allSrcGameObjectMap_;
	},
	getRegistedGameObjectMap : function() {
		return this.allRegistedGameObjectMap_;
	},
	getRegistedCollidableGameObjectMap : function() {
		return this.allRegistedCollidableGameObjectMap_;
	},

	cloneGameObjectByActorID : function(actorID, linkID) {
		if (actorID >= 0) {
			let sdm = ivy.SceneDataManager.getInstance();
			let classID = sdm.getObjectBaseInfo(actorID, ivy.BaseObject.ObjectBaseInfo.eClassID);
			let aiType = sdm.getObjectClassAIID(classID);
			let obj = this.createGameObjectByAIID(aiType);
			if (obj) {
				obj.initWithActorID(actorID);
				obj.initAfterCreate();
				obj.objLinkActorID_ = linkID;
				return obj;
			}
		}
		return null;
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////private:
	//当前场景对象节点
	sceneObjNode_ : null,//cocos2d::Node*
	sceneObjTopNode_ : null,// cocos2d::Node*
	//所有场景中存活对象Map
	allGameObjectMap_ : new LLDictionary(),// std::map<BaseObject*, BaseObject*>
	//场景编辑器中的源对象
	allSrcGameObjectMap_ : {},// std::map<int32_t, BaseObject*>
	//场景中需要特殊处理的对象(横板格斗中为当前激活的战斗对象 提高遍历效率)
	allRegistedGameObjectMap_ : {},// std::map<BaseObject*, BaseObject*>
	//场景中需要碰撞处理的对象
	allRegistedCollidableGameObjectMap_ : {},// std::map<BaseObject*, BaseObject*>
	//具体对象创建函数库(根据不同游戏进行指定)
	objCreateFuncMap_ : new LLDictionary(),// std::map<int32_t, std::function<BaseObject*(void)>>
};


